package com.itogis.interceptors.autoExportExcel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


import jxl.Workbook;
import jxl.biff.DisplayFormat;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteToExcel {
	/*
	public static WritableWorkbook wwb = null;
	
	public static void writeExcel(OutputStream os, List list, boolean flag,
			String columnFields,String columnTitles,String exportFileName) throws IOException, RowsExceededException,
			WriteException {
		
		WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,
				WritableFont.NO_BOLD, false);
		WritableCellFormat formatRight = new WritableCellFormat(wf);
		formatRight.setAlignment(Alignment.RIGHT);
		WritableCellFormat formatCenter = new WritableCellFormat(wf);
		formatCenter.setAlignment(Alignment.CENTRE);
		WritableCellFormat formatLeft = new WritableCellFormat(wf);
		formatLeft.setAlignment(Alignment.LEFT);

		WritableCellFormat formatCenterColor = new WritableCellFormat(wf);
		formatCenterColor.setAlignment(Alignment.CENTRE);
		formatCenterColor.setBackground(jxl.format.Colour.PALE_BLUE);
		formatCenterColor.setBorder(Border.ALL, BorderLineStyle.THIN); 
		formatCenterColor.setVerticalAlignment(VerticalAlignment.CENTRE);

		DisplayFormat displayFormat = NumberFormats.PERCENT_FLOAT;
		WritableCellFormat wcfF = new WritableCellFormat(wf, displayFormat);
		wcfF.setAlignment(Alignment.RIGHT);
		wcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
		jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0");
		jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
		
		jxl.write.NumberFormat nd = new jxl.write.NumberFormat("#,##0.00");
		jxl.write.WritableCellFormat wcfD = new jxl.write.WritableCellFormat(nd);
		
		if (wwb == null) {
			wwb = Workbook.createWorkbook(os);
		}
		
		WritableSheet sheet = wwb.createSheet(exportFileName, 0);
		
		List<Label> labelList=new ArrayList();
		String[] headRows=columnTitles.split("#");
		int headRowNum =headRows.length;			
		String[] fields=columnFields.split(","); 
		int headColNum=fields.length;			 
		List titleList=createHeadTitle(headRows,fields);
		Label label0=null;
		for(int i=0;i<titleList.size();i++){
	    	List cols=(List)titleList.get(i);
	    	for(int k=0;k<cols.size();k++){
	    		label0=new Label(k, i, (String)cols.get(k), formatCenterColor);
				labelList.add(label0);
	    	}
	    } 	
		
		for(int i=0;i<labelList.size();i++){
			sheet.addCell(labelList.get(i));
		}
		mergeTitleCells(sheet,headRows,fields);
		
		
		for (int intRow = 0; intRow < list.size(); intRow++) {
			
			HashMap bean = (HashMap)list.get(intRow);
			//String[] colValue = (String[])tabList1.get(intRow);			
			for (int intCol = 0; intCol < headColNum; intCol++) {
				Label label = null;
				Number number= null;
				String colValue="";
				if(bean.get(fields[intCol])!=null&&!bean.get(fields[intCol]).equals("")) {
					colValue=bean.get(fields[intCol]).toString();
					//String title=titleRows[intCol].split("@")[0];
					if(colValue.matches("[\\d]+")&&intCol!=0){
						number = new jxl.write.Number(intCol,intRow + headRowNum,Double.valueOf(
								colValue).doubleValue(),  wcfN);
						sheet.addCell(number);
					}else if(colValue.matches("[\\d]+\\.[\\d]+")){
						number = new jxl.write.Number(intCol,intRow + headRowNum,Double.valueOf(
								colValue).doubleValue(),  wcfD);
						sheet.addCell(number);
					}else{
						label = new Label(intCol, intRow + headRowNum, bean.get(fields[intCol]).toString(),formatCenter);
						sheet.addCell(label);
					}
				}	
			}
		}
		for(int i=0;i<headColNum;i++){
			sheet.setColumnView(i,17);
		}		
		if (flag == true) {
			wwb.write();			
			wwb.close();
			wwb = null;			
			os.close();
		}
	}
	private static void mergeTitleCells(WritableSheet sheet, String[] headRows,
			String[] fields) throws RowsExceededException,WriteException{
		for(int i=0;i<headRows.length;i++){
			String[] headCols=headRows[i].split(",");
			int sumColSpan=0;
			for(int j=0;j<headCols.length;j++){
				String[] colParams=headCols[j].split("@");
				String title=colParams[0];
				int rowspan=Integer.parseInt(colParams[1]);
				int colspan=Integer.parseInt(colParams[2]);
				sheet.mergeCells(sumColSpan, i, sumColSpan+colspan-1, i+rowspan-1);
				sumColSpan+=colspan;
			}
		}	
		
	}

	private static List createHeadTitle(String[] headerRows, String[] fields) {
		List<List> headRows=new ArrayList();
		List<String> headRowSpanColsTemp=new ArrayList();
	    for(int i=0;i<headerRows.length;i++){
			String[] headerCols=headerRows[i].split(",");
			List<String> headCols=new ArrayList();
			List<String> headRowSpan=new LinkedList();
			int colIndex=-1;
			for(int j=0;j<headerCols.length;j++){
				String[] colParams=headerCols[j].split("@");
				String title=colParams[0];
				int rowspan=Integer.parseInt(colParams[1]);
				int colspan=Integer.parseInt(colParams[2]);
				for(int c=0;c<colspan;c++){
					headCols.add(title);
					colIndex++;
				}	
				for(int r=1;r<rowspan;r++){
					headRowSpanColsTemp.add(i+r+"#"+colIndex+"#"+title);
				}
				
			}
			headRows.add(headCols);
	    }	
	    for(int i=0;i<headRows.size();i++){
	    	List cols=headRows.get(i);
	    	for(int k=0;k<cols.size();k++){
	    		for(int j=0;j<headRowSpanColsTemp.size();j++){
		    		String temp=headRowSpanColsTemp.get(j);
		    		String[] cell=temp.split("#");
		    		int row=Integer.parseInt(cell[0]);
		    		int col=Integer.parseInt(cell[1]);
		    		String title=cell[2];
		    		if(i==row&&k==col){
		    			cols.add(k,title);
		    		}
		    	}
	    	}
	    	
	    }
	    return headRows;
		
	}
	*/
}
